/*
 * @Description: 按钮权限自定义指令
 * @Version: 2.0
 * @Autor: lhl
 * @Date: 2021-06-20 21:24:16
 * @LastEditors: lhl
 * @LastEditTime: 2021-06-20 21:24:59
 */
import { useStore } from 'vuex'
import { Directive } from 'vue'

export const permission: Directive = {
  mounted(el, binding) {
    const { value } = binding
    const roles = useStore().state.user.roles
    if (value && value instanceof Array && value.length > 0) {
      const permissionRoles = value
      const hasPermission = roles.some((role: any) => {
        return permissionRoles.includes(role)
      })
      if (!hasPermission) {
        el.style.display = 'none'
      }
    } else {
      throw new Error("need roles! Like v-permission=\"['admin','editor']\"")
    }
  }
}
